<%@ include file="css/header.jsp" %>
<title>Add group</title>
<%
String userId;
String manageGroups;
if((String)session.getAttribute("userId")==null||((String)session.getAttribute("userId")).trim().length() == 0) userId = "null";
else userId = (String)session.getAttribute("userId");
if((String)session.getAttribute("manageGroups")==null||((String)session.getAttribute("manageGroups")).trim().length() == 0) manageGroups = "null";
else manageGroups = (String)session.getAttribute("manageGroups");
	if(!(userId.equals("null"))){
		if(manageGroups.equals("1")){
%>

<script type="text/javascript">
function navigate(e) {
	if(event.keyCode == 13){  //enter pressed  
	    return false;
		}else 
			return true;
}

document.onkeypress=navigate;

$(document).ready(function() {
	 var pageObj = $("#AddGroup");
     var formObj = pageObj.find("#formAddGroup");
     /**
      * Init Control
      */
     //Form
     formObj.validationEngine('attach');
	$('#mnu li').hover(
	        function () {
	            //show its submenu
	            $('ul', this).slideDown(100);
	 
	        }, 
	        function () {
	            //hide its submenu
	            $('ul', this).slideUp(100);         
	        }
	    );

	$("#btnSave").click(function(){
		var checkGroup = formObj.find("#checkGroup").text();
		if(checkGroup!=""){
			$.msgbox('Please change group\'s name', {
                type : 'error'
            }, function(buttonPressed) {
                // your code here
            });
		}else{
			if(formObj.validationEngine('validate')) {
				
				var data = formObj.serialize();
	            $.ajax({
	                type: 'POST',
	                url: '<%=request.getContextPath()%>/AddGroupServlet/AddNewGroup',
	                data: data,
	                dataType: 'json',
	                success: function(data){
	                    $.each(data, function(key, value) {
	                        //Validate is error send?
	                        if (key == 'error') {
	                            $.msgbox(value, {
	                                type : 'error'
	                            }, function(buttonPressed) {
	                                // your code here
	                            });
	                        } else { //No Error
	                            $.msgbox('Add group success.', {
	                                type : 'info'
	                            }, function(buttonPressed) {
	                                window.location.href = "ManageGroupServlet";
	                            });
	                        }
	                        return false; //break each loop
	                    });
	                },
	                error: function(){
	                    $.msgbox('Error', {
	                        type : 'error'
	                    }, function(buttonPressed) {
	                        // your code here
	                    });
	                }
	            });
			}
		}
		 return false;
	});
		
});   
function checkSelect(reJPMCTCD,reTCDTCD,reJPMCRD,reRDRD,tcdTracking,rdTracking){
	if ((reJPMCTCD.checked == 1)||(reTCDTCD.checked == 1)) tcdTracking.checked=1;
	if ((reJPMCRD.checked == 1)||(reRDRD.checked == 1)) rdTracking.checked=1;
}
</script>

<body>
<script type="text/javascript">
function checkGroup(){
		var group = $("#group").val();
		$.ajax({
	        type: 'POST',
	        url: '<%=request.getContextPath()%>/AddGroupServlet/CheckGroup?group='+group ,
	        dataType: 'json',
	        success: function(data){
	            $.each(data, function(key, value) {
	                //Validate is error send?
	                if (key == 'error') {
	                   $("#checkGroup").text("Group already exists.");
	                } else { 
	                	 $("#checkGroup").text("");
	                }
	                return false; //break each loop
	            });
	        },
	        error: function(){
	            $.msgbox('Error', {
	                type : 'error'
	            }, function(buttonPressed) {
	                // your code here
	            });
	        }
	    }); 
	}

</script>
<div id="jp-main">
        <div class="jp-sheet">
            <div class="jp-sheet-tl"></div>
            <div class="jp-sheet-tr"></div>
            <div class="jp-sheet-bl"></div>
            <div class="jp-sheet-br"></div>
            <div class="jp-sheet-tc"></div>
            <div class="jp-sheet-bc"></div>
            <div class="jp-sheet-cl"></div>
            <div class="jp-sheet-cr"></div>
            <div class="jp-sheet-cc"></div>
            <div class="jp-sheet-body">
                <div class="jp-header">
                    <div class="jp-header-center">
                        <div class="jp-header-png"></div>
                        <div class="jp-header-jpeg"></div>
                    </div>
                        <div class="jp-header-fluid-object"></div>
                </div>
                <div class="jp-nav">
                	<div class="l"></div>
                	<div class="r"></div>
                	<ul class="jp-menu">
                		
                			<%if (session.getAttribute("tcdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServlet" ><span class="l"></span><span class="r"></span><span class="t">TCD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdTracking").equals("1")){%>
					    	  <li>
					    	  	<a href="ViewServletRD"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Tracking</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("tcdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="TCDBillingServlet"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">TCD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (session.getAttribute("rdBilling").equals("1")){%>
					    	  <li>
					    	  	<a href="RDBilling"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">RD Billing</span></a>                		
                			  </li>
					      	<%} %>
					      	<%if (!(userId.equals("0"))){%>
					    	  <li>
					    	  	<a href="EditProfile.jsp"><span class="l"></span><span class="r"></span><span class="t"></span><span class="t">Edit profile</span></a>                		
                			  </li>
					      	<%} %>
                			
           		  		<li>
                			<a href="#" class="active"><span class="l"></span><span class="r"></span><span class="t">User Management</span></a>
							<ul>
								<%if (session.getAttribute("manageUsers").equals("1")){%>
						    		<li><a href="ManageAdminServlet">Manage users</a></li>
						      	<%} %>
						      	<%if (session.getAttribute("manageGroups").equals("1")){%>
					    			<li><a href="ManageGroupServlet">Manage groups</a></li>
					      		<%} %>
					      		<%if (session.getAttribute("userLog").equals("1")){%>
					    			<li><a href="UserLogServlet">User log</a></li>
					      		<%} %>
           					</ul>
               		  	</li>		
           		  	  	<li>
                			<a href="Logout"><span class="l"></span><span class="r"></span><span class="t">Logout</span></a>                		
                	  	</li>
               	  </ul>
                </div>
                <div class="jp-content-layout">
                  <div class="jp-post-body">
                     <h2 class="jp-postheader">Manage groups</h2>
                      <div class="jp-postcontent">
                      <div id="AddGroup">
                     <fieldset id="fsAddAdminPage">
			            <legend class="pageTitleForm"><h3>Add group</h3></legend>
			            <form id="formAddGroup" class="form" >
			                <fieldset>
			                    <ol>
			                        <li class="">
			                            <div class="colTile">
			                              <label for="group">Group name*</label>
			                            </div>
			                            <div class="colContent">
			                                <input type="text" name="group" id="group" value="" class="validate[required]"  OnChange="JavaScript:checkGroup();"/>
			                            <span id="checkGroup"></span>
			                            </div>
			                        </li>
			                      
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="permission">Permission</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="tcdTracking" value="tcdTracking" checked="checked" disabled="disabled">TCD Tracking<br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="rdTracking" value="rdTracking">RD Tracking<br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="tcdBilling" value="tcdBilling">TCD Billing<br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="rdBilling" value="rdBilling">RD Billing<br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="manageUsers" value="manageUsers">Manage users<br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="manageGroups" value="manageGroups">Manage groups<br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="userLog" value="userLog">User log<br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" id = "reTCDTCD" name="reTCDTCD" value="reTCDTCD" onChange="JavaScript:checkSelect(reJPMCTCD,reTCDTCD,reJPMCRD,reRDRD,tcdTracking,rdTracking);">Resend TCD(TCD) <br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="reJPMCTCD" id = "reJPMCTCD" value="reJPMCTCD" OnChange="JavaScript:checkSelect(reJPMCTCD,reTCDTCD,reJPMCRD,reRDRD,tcdTracking,rdTracking);">Resend JPMC(TCD)<br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="reRDRD" id = "reRDRD" value="reRDRD" OnChange="JavaScript:checkSelect(reJPMCTCD,reTCDTCD,reJPMCRD,reRDRD,tcdTracking,rdTracking);">Resend RD(RD)<br>
			                            </div>
			                        </li>
			                        <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               <input type="checkbox" name="reJPMCRD" id = "reJPMCRD" value="reJPMCRD" OnChange="JavaScript:checkSelect(reJPMCTCD,reTCDTCD,reJPMCRD,reRDRD,tcdTracking,rdTracking);">Resend JPMC(RD)<br>
			                            </div>
			                        </li>
			                         <li class="column-2">
			                            <div class="colTile">
			                              <label for="authority">&nbsp;</label>
			                            </div>
			                            <div class="colContent">
			                               &nbsp;<br>
			                            </div>
			                        </li>
			                        <li>
			                            <div  class="colTile">
			                                &nbsp;
			                            </div>
			                            <div class="colContent">
			                                <div id="btnCommand">
			                                  <button id="btnClear" type="reset" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"><span class="ui-button-icon-primary ui-icon ui-icon-refresh"></span><span class="ui-button-text">Clear</span></button>
			                                   <button id="btnSave" type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" ><span class="ui-button-icon-primary ui-icon ui-icon-disk"></span><span class="ui-button-text">Save</span></button>
			                                </div>
			                            </div>
			                        </li>
			                    </ol>
			                </fieldset>
			            </form>
			        	</fieldset>
			        	</div>
                      </div>
                  </div>
                </div>
            </div>
        </div>
</div>
    
</body>	
<%
		}else{
%>
<script type="text/javascript">
    	window.location.href = "EditProfile";
</script>
<%
		}
	}else{
%>
<script type="text/javascript">
	window.location.href = "LoginServlet";
</script>
<%
	}
%>
</html>